package controle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import persistencia.AlunoDAO;
import modelo.Aluno;

/**
 * Servlet implementation class CadastrarAluno
 */
@WebServlet("/cadastrarAluno")
public class CadastrarAluno extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarAluno() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Aluno aluno;
		AlunoDAO alunoDAO;
		String cmd = request.getParameter("cmd");
		if (cmd.equalsIgnoreCase("incluir")){
			try{
				Integer matricula = Integer.valueOf(request.getParameter("matricula"));
				String nome  = request.getParameter("nome");
				aluno = new Aluno(nome, matricula);
				alunoDAO = new AlunoDAO();
				alunoDAO.inserir(aluno);
				
				//Fazer o encaminhamento para a página confirmação
				// request.setAttribute("msg", "Gravado com sucesso!");  
		        // request.getRequestDispatcher("inicial.jsp").forward(request, response);
			}catch(Exception e){
				String erro = "A mátricula :"+request.getParameter("matricula")
						+"ou o nome: "+request.getParameter("nome")
						+"Não são entradas válidas";
				request.setAttribute("erro", erro);
				request.setAttribute("url", "cadastroAluno.jsp");
				request.getRequestDispatcher("/erro").forward(request, response);
			}
		} else if(cmd.equalsIgnoreCase("deletar")){
			try{
				Integer matricula = Integer.valueOf(request.getParameter("matricula"));
				alunoDAO = new AlunoDAO();
				alunoDAO.deletar(matricula);
			}catch(Exception e){
				
			}
		}else if(cmd.equalsIgnoreCase("listarTodos")){
			try{
				
			}catch(Exception e){
				
			}
		}else if(cmd.equalsIgnoreCase("listarUm")){
			try{
				
			}catch(Exception e){
				
			}
		}else if(cmd.equalsIgnoreCase("alterar")){
			try{
				
			}catch(Exception e){
				
			}
		}
		
		
		
		
			
		
	}

}
